<template>
  <div class="page-container">
    <el-card class="content-container">
      <div class="toolbar">
        <div class="toolbar-left">
          <h3>论坛管理</h3>
        </div>
        <div class="toolbar-right">
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增帖子
          </el-button>
        </div>
      </div>

      <!-- 搜索表单 -->
      <el-form :model="searchForm" class="search-form" inline>
        <el-form-item label="帖子标题">
          <el-input v-model="searchForm.forumName" placeholder="请输入帖子标题" clearable />
        </el-form-item>
        <el-form-item label="发帖用户">
          <el-input v-model="searchForm.yonghuName" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="forumName" label="帖子标题" />
        <el-table-column prop="yonghuName" label="发帖用户" width="120" />
        <el-table-column prop="forumContent" label="帖子内容" min-width="200">
          <template #default="scope">
            <div class="content-text">
              {{ scope.row.forumContent }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="zan" label="点赞数" width="100" />
        <el-table-column prop="cai" label="点踩数" width="100" />
        <el-table-column label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.forumStateTypes === 1 ? 'success' : 'danger'">
              {{ scope.row.forumStateTypes === 1 ? '正常' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="发布时间" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleView(scope.row)">
              查看
            </el-button>
            <el-button 
              :type="scope.row.forumStateTypes === 1 ? 'warning' : 'success'" 
              size="small" 
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.forumStateTypes === 1 ? '禁用' : '启用' }}
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; text-align: right;"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const tableData = ref([])

const searchForm = reactive({
  forumName: '',
  yonghuName: ''
})

const pagination = reactive({
  page: 1,
  size: 20,
  total: 0
})

// 模拟数据
const mockData = [
  {
    id: 1,
    forumName: '有机蔬菜种植经验分享',
    yonghuName: '张三',
    forumContent: '大家好，我想分享一下我种植有机蔬菜的一些经验。首先要选择合适的土壤，然后注意施肥和浇水的时机...',
    zan: 15,
    cai: 2,
    forumStateTypes: 1,
    createTime: '2024-08-16 10:30:00'
  },
  {
    id: 2,
    forumName: '如何挑选新鲜水果',
    yonghuName: '李四',
    forumContent: '作为一个水果爱好者，我想和大家分享一些挑选新鲜水果的小技巧。首先看外观，然后闻味道...',
    zan: 8,
    cai: 1,
    forumStateTypes: 1,
    createTime: '2024-08-15 09:20:00'
  }
]

// 方法
const loadData = () => {
  loading.value = true
  
  // 模拟API调用
  setTimeout(() => {
    tableData.value = mockData
    pagination.total = mockData.length
    loading.value = false
  }, 500)
}

const handleSearch = () => {
  pagination.page = 1
  loadData()
}

const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

const handleAdd = () => {
  ElMessage.info('新增帖子功能开发中...')
}

const handleView = (row) => {
  ElMessage.info(`查看帖子: ${row.forumName}`)
}

const handleToggleStatus = (row) => {
  const action = row.forumStateTypes === 1 ? '禁用' : '启用'
  ElMessageBox.confirm(
    `确定要${action}帖子"${row.forumName}"吗？`,
    `确认${action}`,
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    row.forumStateTypes = row.forumStateTypes === 1 ? 2 : 1
    ElMessage.success(`${action}成功`)
  })
}

const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除帖子"${row.forumName}"吗？`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('删除成功')
    loadData()
  })
}

const handleSizeChange = (size) => {
  pagination.size = size
  loadData()
}

const handleCurrentChange = (page) => {
  pagination.page = page
  loadData()
}

// 生命周期
onMounted(() => {
  loadData()
})
</script>

<style lang="scss" scoped>
@import '@/styles/index.scss';

.content-text {
  max-height: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  line-height: 1.4;
}
</style>
